<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.userId"
          placeholder="请输入用户ID"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formInline.valueId"
          placeholder="请输入商品ID"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          icon="el-icon-search"
          >查找</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-download"
          @click="exportTable"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <div class="tables">
      <template>
        <el-table border :data="tableData" style="width: 100%">
          <el-table-column prop="userId" label="用户ID" align="center">
          </el-table-column>
          <el-table-column prop="valueId" label="商品ID" align="center">
          </el-table-column>
          <el-table-column prop="star" label="打分" align="center">
          </el-table-column>
          <el-table-column prop="content" label="评论内容" align="center">
          </el-table-column>
          <el-table-column prop="picUrls" label="评论图片" align="center">
            <template slot-scope="scope">
              <el-image
                v-for="item in scope.row.picUrls"
                :key="item"
                style="width: 40px; height: 40px; margin: 3px"
                :src="item"
                :preview-src-list="scope.row.picUrls"
              >
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="时间" align="center">
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="replys(scope.$index, scope.row)"
              >
                回复
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="dels(scope.$index, scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="get.page"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="get.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ========================= -->
    <el-dialog title="回复" :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="回复内容">
          <el-input
            type="textarea"
            v-model="form.content"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { downloadXlsx } from "../../units/xlx.js";
import { getList, del, reply } from "@/api/commodity/comment";
export default {
  data() {
    return {
      comm: {
        commentId:'',
      },
      flag: true,
      formLabelWidth: "120px",
      dialogVisible: false,
      form: {},
      tableData: [],
      formInline: {},
      total: 0,
      get: {
        page: 1,
        limit: 20,
        userId: "",
        valueId: "",
        sort: "add_time",
        order: "desc",
      },
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    handleChange(file) {
      console.log(file);
      this.getdata();
      this.dialogVisible = false;
    },
    getdata() {
      getList(this.get).then((res) => {
        console.log(res);
        if ((res.data.errmsg = "成功")) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          console.log(this.tableData);
        }
      });
    },
    // 查询
    onSubmit() {
      this.get.page = 1;
      this.get.userId = this.formInline.userId;
      this.get.valueId = this.formInline.valueId;
      this.getdata();
    },

    // 编辑
    handleEdit(a, b) {
      this.flag = true;
      this.dialogVisible = true;
      this.form = b;
    },
    //编辑确定
    update() {
      this.comm.content=this.form.content;
      reply(this.comm).then((res) => {
        console.log(res);
        if (res.errmsg == "成功") {
          console.log(res);
          this.getdata();
          this.form = {};
          this.dialogVisible = false;
          this.$notify({
            title: "成功",
            message: 回复成功,
            type: "success",
          });
        }else{
          this.$notify({
            title: "失败",
            message: res.errmsg,
            type: "error",
          });
        }
      });
    },
    replys(a, b) {
      // console.log(a,b)
      this.comm.commentId = b.id;
      console.log(this.comm);
      this.dialogVisible = true;
      // reply().then((res) => {

      // })
    },
    // 删除
    dels(a, b) {
      del(b).then((res) => {
        if (res.errmsg == "成功") {
          this.getdata();
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    //导出
    exportTable() {
      let headers = [
        "评论ID",
        "用户ID",
        "商品ID",
        "评论",
        "评论图片列表",
        "评论时间",
      ];
      let list = [headers];
      for (let i = 0; i < this.tableData.length; i++) {
        let arr = [
          this.tableData[i].id,
          this.tableData[i].userId,
          this.tableData[i].valueId,
          this.tableData[i].content,
          this.tableData[i].picUrls,
          this.tableData[i].updateTime,
        ];
        list.push(arr);
      }
      downloadXlsx(list, "存储对象.xlsx");
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.get.limit = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.get.page = val;
      this.getdata();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table td,
::v-deep .el-table th {
  padding: 4px 0px;
}
.el-table {
  font-size: 12px;
}
.page {
  margin-top: 40px;
  display: flex;
  justify-content: left;
}
</style>